<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@include file="../includes/tag.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">    
    <title>业务信息管理平台</title>
    <script type="text/javascript" src="<%=basePath%>resources/lib/jquery/jquery.js"></script>
    <script type="text/javascript" src="<%=basePath%>resources/lib/webui/buss.ui.core.js"></script>
    <script type="text/javascript" src="<%=basePath%>resources/lib/webui/buss.ui.js"></script>
	<script type="text/javascript" src="<%=basePath%>resources/lib/jqgrid/js/i18n/grid.locale-cn.js"></script>
	<script type="text/javascript" src="<%=basePath%>resources/lib/jqgrid/js/jquery.jqGrid.min.js"></script>
	<script type="text/javascript" src="<%=basePath%>resources/lib/public.js"></script>
	
	<link rel="stylesheet" type="text/css" href="<%=basePath%>resources/lib/styles/themes/buss-ui.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath%>resources/lib/jqgrid/css/jquery-ui.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath%>resources/lib/jqgrid/css/ui.jqgrid.css">
    <link rel="stylesheet" type="text/css" href="<%=basePath%>resources/styles/public.css">
    <link rel="stylesheet" type="text/css" href="<%=basePath%>resources/styles/icon.css">
    
    <script type="text/javascript">
    	$(document).ready(function(){
    		
    		setGridData();
    		resize();
    		$(window).resize(resize);
    		
    		$("#search-btn").button({iconCls:'icon-search',handler:function(){
    			var jsonParam = syscore.formToJson($("form[name='_searchForm']"));
				$("#tabledata").jqGrid("setGridParam",{page:1,postData:jsonParam}).trigger("reloadGrid");
			}});
    		
    		$("#search-clear").button({iconCls:'icon-cancel',handler:function(){
    			$("form[name='_searchForm']").resetForm();
    			$("#search-btn").trigger("click");
    		}});
    		
    		$("#add-btn").button({iconCls:'icon-add',handler:function(){
    			editCompany("addpage","");
			}});
    		
			$("#edit-btn").button({iconCls:'icon-edit',handler:function(){
				var selRows = $("#tabledata").jqGrid('getGridParam',"selarrrow");
				if(selRows.length == 0){
					$.alert('提示','你没有选择要修改的单位信息!',300,140,'info');
					return;
				}
				if(selRows.length > 1){
					$.alert('提示','只能选择一条数据，请重新选择!',300,140,'info');
					return;
				}
				editCompany("editpage",selRows[0]);
			}});
			
			$("#del-btn").button({iconCls:'icon-remove',handler:function(){
				var selRows = $("#tabledata").jqGrid('getGridParam',"selarrrow");
				if(selRows.length == 0){
					$.alert('提示','你没有选择要修改删除的单位信息!',300,140,'info');
					return;
				}
				$.confirm('提示','确认要删除选中数据吗?',300,140,function(){
					var str = "";
					for(var i = 0;i < selRows.length;i++){
						if(i < selRows.length - 1){
							str += "ids=" + selRows[i] + "&";
						}else{
							str += "ids=" + selRows[i];
						}
					}
					$.post("<%=basePath%>root/company/delete.do",str,function(rsData){
						if(rsData.message.code != '0'){
							$.alert('错误','删除单位信息失败!原因:' + rsData.message.mess,300,140,'error');
							return;
						}
						$("#tabledata").jqGrid({page:1}).trigger('reloadGrid');
					},"json");
				});	
			}});
			
			$("#change-status-disable").button({iconCls:'icon-remove',handler:function(){
				var selRows = $("#tabledata").jqGrid('getGridParam',"selarrrow");
				if(selRows.length == 0){
					$.alert('提示','你没有选择停用的单位信息!',300,140,'info');
					return;
				}
				var str = "";
				for(var i = 0;i < selRows.length;i++){
					if(i < selRows.length - 1){
						str += "ids=" + selRows[i] + "&";
					}else{
						str += "ids=" + selRows[i];
					}
				}
				$.post("<%=basePath%>root/company/updatestatus/disabled.do",str,function(rsData){
					if(rsData.message.code != '0'){
						$.alert('错误','停用失败!原因:' + rsData.message.mess,300,140,'error');
						return;
					}
					//var datas = $("#tabledata").jqGrid("getGridParam",'selarrrow');
					for(var i = 0;i < selRows.length;i++){
						$("#tabledata").jqGrid("setRowData",selRows[i],{status:'disabled'});
					}
				});
			}});
			
			$("#change-status-normal").button({iconCls:'icon-remove',handler:function(){
				var selRows = $("#tabledata").jqGrid('getGridParam',"selarrrow");
				if(selRows.length == 0){
					$.alert('提示','你没有选择要启用的单位信息!',300,140,'info');
					return;
				}
				var str = "";
				for(var i = 0;i < selRows.length;i++){
					if(i < selRows.length - 1){
						str += "ids=" + selRows[i] + "&";
					}else{
						str += "ids=" + selRows[i];
					}
				}
				$.post("<%=basePath%>root/company/updatestatus/normal.do",str,function(rsData){
					if(rsData.message.code != '0'){
						$.alert('错误','启用失败!原因:' + rsData.message.mess,300,140,'error');
						return;
					}
					for(var i = 0;i < selRows.length;i++){
						$("#tabledata").jqGrid("setRowData",selRows[i],{status:'normal'});
					}
					//$("#tabledata").jqGrid().trigger('reloadGrid');
				});
			}});
    	});
    
    	function editCompany(type,companyId){
    		var url = "<%=basePath %>root/company/";
    		var pageUrl = "";
    		var saveUrl = "";
    		var title = "";
    		if(type == "editpage"){
    			pageUrl = url + "editpage.do?id=" + companyId;
    			title = "修改单位信息"
    		}else{
    			pageUrl += url + "addpage.do";
    			title = "新增单位信息";
    		}
    		var dlg = $("<div/>").dialog({
    			modal:true,
    			header:true,
    			height:400,
    			width:600,
    			title:title,
    			iconCls:'icon',
    			loadUrl:pageUrl,
    			buttons:[
    			         {text:"确  定",type:'button',iconCls:'icon-save',handler:function(){
    			        	 if(type == "editpage"){
    			        		 saveUrl = url + "edit.do";
    			        	 }else{
    			        		 saveUrl = url + "add.do";    			        		 
    			        	 }
    			        	 dlg.find("form").attr("action",saveUrl);
    			        	 syscore.ajaxSubmit(dlg.find("form"),function(rsData){
    			        		 if(rsData.message.code != '0'){
    								 $.alert('提示','保存数据失败!原因:' + rsData.message.mess,300,140,'error');
    								 return false;
    							 }else{						 			
    		        			 	 $("#tabledata").jqGrid().trigger('reloadGrid');			        			 	
    			       			 	 dlg.dialog("destroy");
    							 }	
    			        	 });
    			         }},
    			         {text:'关  闭',type:'button',iconCls:'icon-cancel',handler:function(){
    			        	 dlg.dialog("destroy");
    			         }}
    			        ],
    			onLoad:function(){
    				if(type == "editpage"){
    					dlg.find("input[name='id']").attr("readonly","true");
    	    		}
    				dlg.find("#companyedit-tabs ul").idTabs("company_base");
    				dlg.find("input[name='name']").focusout(function(){
    					var hanzi = $(this).val();
    					$.post("<%=basePath%>core/hanzitopinyin.do",{hanzi:hanzi},function(rsData){
    						if(rsData.message.code != 0){
    							dlg.find("input[name='pinyin']").val("");
    						}else{
    							dlg.find("input[name='pinyin']").val(rsData.pinyin);    							
    						}
    					},"json");
    				});
    			}
    		});
    		dlg.dialog("loadPage");
    	};
    	
    	function setGridData(){
    		var url = "<%=basePath%>root/company/list.do";
    		var colNames = ["单位编码","单位名称","联系人","联系电话","当前状态","单位地址"];
    		var colModel = [
    		                {name:'id',index:'id',width:60,align:'left'},
    		                {name:'name',index:'name',width:150,align:'left'},
    		                {name:'contact',index:'contact',width:100,align:'left'},
    		                {name:'contactPhone',index:'contactPhone',width:100,align:'left'},
    		                {name:'status',index:'status',width:60,align:'center',formatter:syscore.formaterStatus},
    		                {name:'address',index:'address',width:200,align:'left'}
    		               ];
    		syscore.setGridData("#tabledata","#tablepage",url,colNames,colModel,true,0,'id');
    	};
    	
    	function resize(){
    		$("#p_container").width($(window).width());
			var h = $(window).height() - $(".nav_search").outerHeight() - $(".nav_btn_bar").outerHeight() - 10;
			$(".context").height(h);
			$("#tabledata").jqGrid("setGridHeight",h - 50);
			$("#tabledata").jqGrid("setGridWidth",$(window).width() - 12);
		}
    </script>
</head>
<body>
  	<div  id="p_container" style="height:100%;position:relative;">
	    <div class="nav_search" style="height: 28px; padding-top: 20px;">
	   		<form action="" name="_searchForm">
		    	<table height="100%" cellpadding="0" cellspacing="0">
		    		<tr>
		    			<td valign="middle">公司/单位名称：</td>
		    			<td valign="middle">
		    				<input type="text" size="25" maxlength="100" class="text" name="name"/>
		    			</td>
		    			<td valign="middle" style="padding-left:10px;">拼音码：</td>
		    			<td valign="middle">
		    				<input type="text" size="25" maxlength="100" class="text" name="pinyin"/>
		    			</td>
		    			<td valign="middle" style="padding-left:10px;">公司/单位地址：</td>
		    			<td valign="middle">
		    				<input type="text" size="25" maxlength="100" class="text" name="address"/>
		    			</td>
		    			<td style="padding-left:5px;">
		    				<a id="search-btn" href="javascript:void(0);">查 询</a>
		    			</td>
		    			<td style="padding-left:5px;">
		    				<a id="search-clear" href="javascript:void(0);">清 空</a>
		    			</td>
		    		</tr>
		    	</table>
	    	</form>
	    </div>
	    <div class="nav_btn_bar">
	    	<table height="100%" cellpadding="0" cellspacing="0">
	    		<tr>
	    			<td valign="middle">
	    				<a id="add-btn" href="javascript:void(0);">新 增</a>
	    			</td>
	    			<td valign="middle"" style="padding-left:5px;">
	    				<a id="edit-btn" href="javascript:void(0);">修 改</a>
	    			</td>
	    			<td valign="middle" style="padding-left:5px;">
	    				<a id="del-btn" href="javascript:void(0);">删 除</a>
	    			</td>
	    			<td valign="middle" style="padding-left:5px;">
	    				<a id="change-status-disable" href="javascript:void(0);">停 用</a>
	    			</td>
	    			<td valign="middle" style="padding-left:5px;">
	    				<a id="change-status-normal" href="javascript:void(0);">启 用</a>
	    			</td>
	    		</tr>
	    	</table>
	    </div>
	    <div class="context">
	    	<table id="tabledata"></table>
	    	<div id="tablepage"></div>
	    </div>
  	</div> 
    
  </body>
</html>